package com.tungse.juc.queue;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author Tungse
 */
public class SynchronousQueue_Demo {

    public static void main(String[] args) {
        SynchronousQueue<String> queue = new SynchronousQueue<>();

        new Thread(() -> {
            try {
                queue.put("1");
                System.out.println(Thread.currentThread().getName() + " put 1");
//                TimeUnit.SECONDS.sleep(3);
                queue.put("2");
                System.out.println(Thread.currentThread().getName() + " put 2");
//                TimeUnit.SECONDS.sleep(3);
                queue.put("3");
                System.out.println(Thread.currentThread().getName() + " put 3");
//                TimeUnit.SECONDS.sleep(3);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "A").start();
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + " poll 1" + queue.take());
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + " poll 2" + queue.take());
                TimeUnit.SECONDS.sleep(3);
                System.out.println(Thread.currentThread().getName() + " poll 3" + queue.take());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "B").start();
    }
}
